﻿using nplJobsCommon.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Select
{
    public class SelectCity : DataAccessBase
    {
        public int CountryID { get; set; }
        public int DistrictID { get; set; }

        public SelectCity()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectCity.ToString();
        }

        #region GetCity
        public List<City> GetCity()
        {
            DataBaseHelper dbHelper;
            DataSet dsCity;

            try
            {
                SelectCityParameters selecCityParamenters = new SelectCityParameters(this.CountryID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selecCityParamenters.Parameters;
                dsCity = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsCity);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<City> GetCityByDistrict()
        {
            DataBaseHelper dbHelper;
            DataSet dsCity;

            try
            {
                SelectDistrictParameters selecCityParamenters = new SelectDistrictParameters(this.DistrictID);
                dbHelper = new DataBaseHelper(StoredProcedure.Name.spGetCity.ToString());
                dbHelper.Parameters = selecCityParamenters.Parameters;
                dsCity = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsCity);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion



        #region SQL parameters for selecting cities by country
        public class SelectCityParameters
        {
            public int CkeckUnique;
            private SqlParameter[] _parameters;

            public SelectCityParameters(int countryID)
            {
                this.CountryID = countryID;
                Build();
            }

            #region spAddContact parameters
            /*
         
         */
            #endregion

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@CountryID", CountryID),
                };

                Parameters = parameters;
            }

            public int CountryID { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }

        public class SelectDistrictParameters
        {
            public int CkeckUnique;
            private SqlParameter[] _parameters;

            public SelectDistrictParameters(int districtID)
            {
                this.DistrictID = districtID;
                Build();
            }

            #region spAddContact parameters
            /*
         
         */
            #endregion

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@DistrictID", DistrictID),
                };

                Parameters = parameters;
            }

            public int DistrictID { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion
        #region GetList
        private List<City> GetList(DataSet dsCity)
        {
            List<City> cities;
            try
            {
                cities = (from n in dsCity.Tables[0].AsEnumerable()
                          select new City
                                {
                                    ID = n.Field<int>("ID"),
                                    Name = n.Field<string>("Name")
                                }).ToList();

                return cities;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}
